1468C - Berpizza - CodeForces Solution


data structures implementation *1400

Please click on ads to support us..

Python Code:

import heapq

q = int(input())

mono_queue = []
poly_queue = []
served = dict()
ans = []
count = 0
for query in range(1, q+1):
    read_line = input().split(" ")
    if read_line[0] == '1':
        m = int(read_line[1])
        count += 1
        heapq.heappush(mono_queue, (count, -m))
        heapq.heappush(poly_queue, (-m, count))
    elif read_line[0] == '2':
        next_to_serve = heapq.heappop(mono_queue)[0]
        while(next_to_serve in served.keys()):
            next_to_serve = heapq.heappop(mono_queue)[0]
        ans.append(next_to_serve)
        served[next_to_serve] = True

    elif read_line[0] == '3':
        next_to_serve = heapq.heappop(poly_queue)[1]
        while(next_to_serve in served.keys()):
            next_to_serve = heapq.heappop(poly_queue)[1]
        ans.append(next_to_serve)
        served[next_to_serve] = True
print(" ".join([str(x) for x in ans]))

  	 		 						 	    	 		 		   		

C++ Code:

#include <bits/stdc++.h>
#define loop(n) for(int i = 1; i <= n; i++)
#define rloop(n) for(int i = n; i >= 1; i--)
#define loop0(n) for(int i = 0; i < n; i++)
#define rloop0(n) for(int i = n-1; i >= 0; i--)
#define iloop(s,n,step) for(int i = s; i <= n; i+=step)
#define jloop(s,n,step) for(int j = s; j <= n; j+=step)
#define loopit(x) for(auto it = x.begin(); it!= x.end(); it++)
#define all(x) x.begin(),x.end()
using namespace std;
void fast(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
}
bool vis[500009];
int main()
{
    fast();
    int tc = 1;
    //cin >> tc;
    while(tc--){
        int n,q,x,idx=1,order=1;
        cin >> n;
        priority_queue<pair<int,int>> pq;
        while(n--){
            cin >> q;
            if(q==1){
                cin >> x;
                pq.push({x,-idx});
                idx++;
            }
            else if(q==2){
                while(vis[order]) order++;
                cout << order << ' ';
                vis[order]=1;
            }
            else{
                while(vis[-pq.top().second]) pq.pop();
                x=-pq.top().second;
                cout << x << ' ';
                vis[x]=1;
                pq.pop();
            }
        }
    }
    return 0;
}

	 		   	 		   	 										  			


Comments

Submit
0 Comments
More Questions

931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23
1401B - Ternary Sequence
598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane
154B - Colliders
127B - Canvas Frames
107B - Basketball Team
245A - System Administrator
698A - Vacations